from django.core.paginator import Paginator
from django.views.decorators.http import require_GET, require_POST
from utils.customer_util import CustomerUtil

from utils.response_code import ResponseCode
from .models import Source


# Create your views here.

@require_GET
def create_token(request):
    # 创建七牛云token 用于前端文件上传   ()
    key, token = CustomerUtil.create_qiniu_token()
    return CustomerUtil.response_json({
        'key': key,
        'token': token,
        "code": ResponseCode.BUSINESS_SUCCESS
    })


@require_POST
def source_page(request):
    params = CustomerUtil.get_request_params(request)
    page = params.get('page')
    size = params.get('size')

    source_list = Source.objects.all().order_by('-id')
    paginator = Paginator(source_list, size)
    # 数据总条数
    total = paginator.count
    current_page = paginator.page(page)
    # 当前页数据
    table_date = list(current_page.object_list.values())

    return CustomerUtil.response_json({
        'total': total,
        'table_data': table_date,
        'code': ResponseCode.BUSINESS_SUCCESS
    })


@require_POST
def source_add(request):
    form_data = CustomerUtil.get_request_params(request)
    # 获取发送请求的用户  token
    token = request.headers['X-Token']
    try:
        decode_dict = CustomerUtil.decode_token(token)
        # 添加发布人
        form_data['publisher'] = decode_dict['username']
    except Exception as e:
        return CustomerUtil.response_json({
            'code': ResponseCode.BUSINESS_ERROR,
            'message': str(e)
        })
    #  保存文件信息
    Source(**form_data).save()
    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        'message': '文件上传成功'
    })


@require_GET
def source_delete(request, id):
    """
    删除资源信息
     1. 数据库中的记录    id
     2. 七牛云空间中的文件  根据id查询文件 key  cover_key
    """
    source = Source.objects.filter(id=id).first()
    key = source.key
    cover_key = source.cover_key
    # 删除空间数据
    if key:
        CustomerUtil.remove_source(key)
    if cover_key:
        CustomerUtil.remove_source(cover_key)
    # 删除数据库记录
    source.delete()

    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        'message': "资源已移除"
    })


@require_POST
def source_online(request):
    params = CustomerUtil.get_request_params(request)
    page = params.get('page')
    size = params.get('size')

    source_list = Source.objects.filter(file_type='video').order_by('-id')
    paginator = Paginator(source_list, size)
    # 数据总条数
    total = paginator.count
    current_page = paginator.page(page)
    # 当前页数据
    table_date = list(current_page.object_list.values())

    return CustomerUtil.response_json({
        'total': total,
        'table_data': table_date,
        'code': ResponseCode.BUSINESS_SUCCESS
    })


@require_GET
def source_update_view(request, id):
    source = Source.objects.filter(id=id).first()
    Source.objects.filter(id=id).update(views=source.views + 1)

    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        'messages': '浏览量更新成功'
    })
